<template>
    <div class="company">
        <div class="search-term">
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
                <el-form-item label="公司名称">
                    <el-input placeholder="搜索条件" v-model="searchInfo.company_name" clearable></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="searchInfo.status" clearable placeholder="请选择">
                        <el-option
                                v-for="dict in statusOptions"
                                :key="dict.item_value"
                                :label="dict.item_name"
                                :value="dict.item_value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button @click="handleQuery" icon="el-icon-search" type="primary">查询</el-button>
                </el-form-item>
            </el-form>
            <el-form>
                <el-form-item>
                    <el-button @click="createSysCompany" type="primary">新增公司</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                :data="tableData"
                border
                ref="multipleTable"
                row-key="ID"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
        >
            <el-table-column label="ID" width="50" prop="ID">
                <template slot-scope="scope">{{scope.row.ID}}</template>
            </el-table-column>

            <el-table-column label="公司名称" prop="company_name" width="120"></el-table-column>
            <el-table-column label="公司编码" prop="company_code" width="160"></el-table-column>
            <el-table-column label="状态" min-width="60">
                <template slot-scope="scope">{{scope.row.status | basecode(scope.row.status,'sys_common_status')}}</template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" width="320"></el-table-column>
            <el-table-column label="创建时间" width="200">
                <template slot-scope="scope">{{scope.row.CreatedAt | formatDate}}</template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button @click="updateSysCompany(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
                    <el-button @click="deleteSysCompany(scope.row)" type="danger" icon="el-icon-delete" style="margin-left: 6px;" size="mini" >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                :current-page="page"
                :page-size="pageSize"
                :page-sizes="[10, 30, 50, 100]"
                :style="{float:'right',padding:'20px'}"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <el-dialog :before-close="handleClose"  :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" >
            <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="110px">
                <el-form-item label="公司名称" prop="company_name">
                    <el-input autocomplete="off" v-model="formData.company_name" placeholder="请输入公司名称(中文)"  :style="{width: '90%'}"></el-input>
                </el-form-item>
                <el-form-item label="公司编码" prop="company_code">
                    <el-input autocomplete="off" v-model="formData.company_code" placeholder="请输入公司编码(数字)"  :style="{width: '90%'}"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="formData.status">
                        <el-radio
                                v-for="dict in statusOptions"
                                :key="dict.item_value"
                                :label="dict.item_value |formatBoolean"
                        >{{ dict.item_name }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input autocomplete="off" v-model="formData.remark" placeholder="请输入备注"  type="textarea" :style="{width: '90%'}"></el-input>
                </el-form-item>
            </el-form>

            <div class="dialog-footer" slot="footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button @click="enterDialog" type="primary">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
    import {
        createCompany,
        deleteCompany,
        updateCompany,
        getCompanyList
    } from "@/api/company";  //  此处请自行替换地址
    import infoList from "@/mixins/infoList";
    export default {
        name: "Company",
        mixins: [infoList],
        data() {
            return {
                statusOptions: [],
                listApi: getCompanyList,
                dialogFormVisible: false,
                dialogTitle: "新增配置",
                dialogType: "add",
                visible: false,
                searchInfo: {
                    company_name: undefined,
                    status: undefined
                },
                type: "",
                formData: {
                    company_name: "",
                    company_code: "",
                    status:true,
                    remark: ""
                },
                rules: {
                    company_name: [{
                        required: true,
                        message: '请输入公司名（中）',
                        trigger: 'blur'
                    }],
                    company_code: [{
                        required: true,
                        message: '请输入公司编码',
                        trigger: 'blur'
                    }],
                    remark: [{
                        required: true,
                        message: '请输入描述',
                        trigger: 'blur'
                    }],
                },
            };
        },
        filters: {
            formatBoolean: function(s) {
                if (s == "1" ) {
                    return true
                } else {
                    return false;
                }
            }
        },
        methods: {
            //条件搜索前端看此方法
            handleQuery() {
                if (this.searchInfo.status==""){
                    this.searchInfo.status=null
                }
                this.getTableData()
            },
            handleClose(done) {
                // this.initForm();
                done();
            },
            async updateSysCompany(row) {
                this.type = "update";
                this.formData = row;
                this.dialogFormVisible = true;
                this.dialogTitle = '编辑公司';
            },
            async createSysCompany() {
                this.type = "create";
                this.dialogFormVisible = true;
                this.initForm()
                this.dialogTitle = '新增公司';
            },
            initForm() {
                // if (this.$refs.elForm) {
                //     this.$refs.elForm.resetFields();
                // }
                this.formData = {
                    company_name: "",
                    company_code: "",
                    status: true,
                    remark: "",
                };
            },
            closeDialog() {
                // this.initForm();
                this.dialogFormVisible = false;
            },
            // 删除角色
            deleteSysCompany(row) {
                this.$confirm("此操作将永久删除该公司, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(async () => {
                        this.visible = false;
                        const res = await deleteCompany({ ID: row.ID });
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                          await  this.getTableData();
                        }
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            async enterDialog() {
                this.$refs['elForm'].validate(async valid => {
                    if (!valid) return
                    let res;
                    switch (this.type) {
                        case "create":
                            res = await createCompany(this.formData);
                            break;
                        case "update":
                            res = await updateCompany(this.formData);
                            break;
                        default:
                            res = await createCompany(this.formData);
                            break;
                    }
                    if (res.code == 0) {
                        this.closeDialog();
                        await this.getTableData();
                    }
                })
            },
        },
        async created() {
            await this.getTableData();
            this.statusOptions = this.getDicItemList('sys_common_status')
        }
    };
</script>

<style scoped lang="scss">
    .company {
    .el-input-number {
        margin-left: 15px;
    span {
        display: none;
        }
    }
    .button-box {
        padding: 10px 20px;
    .el-button {
        float: right;
    }
    }
    }
    .link-type, .link-type:focus {
        color: #337ab7;
        cursor: pointer;
    }
</style>
